home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mh / mh-6.8 / support / pop / syslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  3.6 KB  |  164 lines

  1. #if !defined (BSD43) && !defined(hpux)
  2. #ifndef lint
  3. static char SccsId[] =    "@(#)syslog.c    4.1 (Berkeley) 5/27/83";
  4. #endif
  5. #ifndef    lint
  6. static char ident[] = "@(#)$Id: syslog.c,v 1.8 1992/12/15 00:20:22 jromine Exp $";
  7. #endif    /* lint */
  8.  
  9. /*
  10.  * SYSLOG -- print message on log file
  11.  *
  12.  * This routine looks a lot like printf, except that it
  13.  * outputs to the log file instead of the standard output.
  14.  * Also, it prints the module name in front of lines,
  15.  * and has some other formatting types (or will sometime).
  16.  * Also, it adds a newline on the end of messages.
  17.  *
  18.  * The output of this routine is intended to be read by
  19.  * /etc/syslog, which will add timestamps.
  20.  */
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24.  
  25. #include "syslog.h"
  26. #include <netdb.h>
  27.  
  28. #define    MAXLINE    1024        /* max message size */
  29. #define BUFSLOP    20        /* space to allow for "extra stuff" */
  30. #ifndef NULL
  31. #define NULL    0        /* manifest */
  32. #endif
  33.  
  34. #define LOG_COOLIT    LOG_LOCAL0    /* local syslog code */
  35. #define LOG_DGRAM    LOG_LOCAL1    /* idem */
  36.  
  37. #ifndef    LOG_HOST
  38. #define LOG_HOST    "localhost"    /* host where syslogd is running */
  39. #endif    /* LOG_HOST */
  40.  
  41. int    LogFile = -1;        /* fd for log */
  42. int    LogStat    = 0;        /* status bits, set by initlog */
  43. char    *LogTag = (char *)NULL;    /* string to tag the entry with */
  44. int    LogMask = LOG_DEBUG;    /* lowest priority to be logged */
  45.  
  46. struct sockaddr_in SyslogAddr;
  47. static char *SyslogHost = LOG_HOST;
  48.  
  49. extern    int errno, sys_nerr;
  50. extern    char *sys_errlist[];
  51.  
  52. syslog(pri, fmt, p0, p1, p2, p3, p4)
  53.     int pri;
  54.     char *fmt;
  55. {
  56.     char buf[MAXLINE+BUFSLOP], outline[MAXLINE + 1];
  57.     register char *b, *f;
  58.  
  59.     if (LogFile < 0)
  60.         openlog(0, 0);
  61.     /* see if we should just throw out this message */
  62.     if (pri > LogMask)
  63.         return;
  64.     for (b = buf, f = fmt; f && *f; b = buf) {
  65.         register char c;
  66.  
  67.         if (pri > 0 && (LogStat & LOG_COOLIT) == 0) {
  68.             sprintf(b, "<%d>", pri);
  69.             b += strlen(b);
  70.         }
  71.         if (LogStat & LOG_PID) {
  72.             sprintf(b, "%d ", getpid());
  73.             b += strlen(b);
  74.         }
  75.         if (LogTag) {
  76.             sprintf(b, "%s: ", LogTag);
  77.             b += strlen(b);
  78.         }
  79.         while ((c = *f++) != '\0' && c != '\n' && b < buf + MAXLINE) {
  80.             if (c != '%') {
  81.                 *b++ = c;
  82.                 continue;
  83.             }
  84.             c = *f++;
  85.             if (c != 'm') {
  86. #ifndef    notdef
  87.                 *b++ = '%', *b++ = c, *b++ = '\0';
  88. #else
  89.                 *b++ = '%', *b++ = c;
  90. #endif
  91.                 continue;
  92.             }
  93.             if ((unsigned)errno > sys_nerr)
  94.                 sprintf(b, "error %d", errno);
  95.             else
  96.                 strcat(b, sys_errlist[errno]);
  97.             b += strlen(b);
  98.         }
  99.         if (c == '\0')
  100.             f--;
  101.         *b++ = '\n', *b = '\0';
  102.         sprintf(outline, buf, p0, p1, p2, p3, p4);
  103.         errno = 0;
  104.         if (LogStat & LOG_DGRAM)
  105.             (void) sendto(LogFile, outline, strlen(outline), 0,
  106.                    &SyslogAddr, sizeof SyslogAddr);
  107.         else
  108.             (void) write(LogFile, outline, strlen(outline));
  109.         if (errno)
  110.             perror("syslog: sendto");
  111.     }
  112. }
  113.  
  114. /*
  115.  * OPENLOG -- open system log
  116.  */
  117. openlog(ident, logstat)
  118.     char *ident;
  119.     int logstat;
  120. {
  121.     struct servent *sp;
  122.     struct hostent *hp;
  123.  
  124.     LogTag = ident;
  125.     LogStat = logstat;
  126.     if (LogFile >= 0)
  127.         return;
  128.     sp = getservbyname("syslog", "udp");
  129.     hp = gethostbyname(SyslogHost);
  130.     if (sp == NULL || hp == NULL)
  131.         goto bad;
  132.     LogFile = socket(AF_INET, SOCK_DGRAM, 0);
  133.     if (LogFile < 0) {
  134.         perror("syslog: socket");
  135.         goto bad;
  136.     }
  137.     bzero(&SyslogAddr, sizeof SyslogAddr);
  138.     SyslogAddr.sin_family = hp->h_addrtype;
  139.     bcopy(hp->h_addr, (char *)&SyslogAddr.sin_addr, hp->h_length);
  140.     SyslogAddr.sin_port = sp->s_port;
  141.     LogStat |= LOG_DGRAM;
  142.     return;
  143. bad:
  144.     LogStat |= LOG_COOLIT;
  145.     LogStat &= ~LOG_DGRAM;
  146.     LogMask = LOG_CRIT;
  147.     LogFile = open("/dev/console", 1);
  148.     if (LogFile < 0) {
  149.         perror("syslog: /dev/console");
  150.         LogFile = 2;
  151.     }
  152. }
  153.  
  154. /*
  155.  * CLOSELOG -- close the system log
  156.  */
  157. closelog()
  158. {
  159.  
  160.     (void) close(LogFile);
  161.     LogFile = -1;
  162. }
  163. #endif    /* not BSD43 */
  164.